package com.example.t470p.facechecktickets;


import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import com.example.t470p.facechecktickets.Utils.SPUtils;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.Call;

public class HeartBeatService extends Service {
    Handler handler = new Handler();
    Runnable runnable_httpRequest;
    String timestamp;

    public HeartBeatService() {

        httpRequest();
    }

    private void httpRequest() {
        runnable_httpRequest = new Runnable() {
            @Override
            public void run() {

                heartbeat();
                handler.postDelayed(runnable_httpRequest, 30 * 1000);
            }
        };
        handler.postDelayed(runnable_httpRequest, 30 * 1000);
    }

    private void heartbeat() {


        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        timestamp = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        Log.d("heart :", timestamp);
//        OkHttpUtils
//                .get()
//                .url(url)
//                .addParams("gateNos", "admin")
//                .addParams("timestamp", timestamp)
//                .addParams("sign", "DDCA01EF3FF8B6C429D8FE406740539F")
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//                        Log.d("heart :", "failed");
//                    }
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        Log.d("heart :", "success");
//                    }
//                });

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    // 先封装一个 JSON 对象
                    JSONObject param = new JSONObject();
                    param.put("gateNos", String.valueOf(SPUtils.get(getBaseContext(),"gateNo","")));
                    param.put("timestamp", timestamp);
                    param.put("sign", "DDCA01EF3FF8B6C429D8FE406740539F");
//
//
//                    String ss = httpUtils.postJson("http://"+ String.valueOf(SPUtils.get(getBaseContext(),"ipString","")) + BaseUrl.HEARTBEAT, param);
                    String ss = httpUtils.postJson("http://"+ MyApplication.m_Url + BaseUrl.HEARTBEAT, param);

                  Log.d("heart",ss);
                } catch (Exception e) {

                }

            }
        }).start();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable_httpRequest);
    }
}
